
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

public class ThreeThreadPrinter {

    //private static Object lock = new Object();
    private static int currentIndex = 0;
    private static String[] tNames = {"A", "B", "C"};

    public static void main(String[] args) {

        //Scanner in = new Scanner(System.in);
        //int a = in.nextInt();
        //System.out.println(a);
        WeakReference<Object> wt;
        ReferenceQueue<Object> queue = new ReferenceQueue<Object>() {

        };
        
        PhantomReference<Object> pt = new PhantomReference<>(new Object(), queue);

        PrintThread p1 = new PrintThread(tNames[0]);
        PrintThread p2 = new PrintThread(tNames[1]);
        PrintThread p3 = new PrintThread(tNames[2]);
        p1.start();
        p2.start();
        p3.start();

        //System.out.println("Hello World!");
    }

    static class PrintThread extends Thread {
        private String mName;
        private boolean isPaused = true;
        public PrintThread(String name) {
            mName = name;
        }
        public void run() {
            while(true) {
                if (isAvaiable(mName)) {
                    System.out.print(mName);
                    incraseIndex();
                }
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                }
            }
        }
    }

    public static boolean isAvaiable(String currentName) {
        boolean isMatched = tNames[currentIndex] == currentName;
        return isMatched;
    }

    public static void incraseIndex() {
        currentIndex = (currentIndex + 1) % tNames.length;
    }
}
